#include "multiwin.h"
#include "init.h"

int initWins(SdlWindow wins[], int len){
    int i;
    for(i = 0; i < len; i++){
        wins[i].win = NULL;
        wins[i].render = NULL;
        wins[i].texture = NULL;
        wins[i].width = WINDOW_WIDTH;
        wins[i].height = WINDOW_HEIGHT;
        wins[i].windowID = 0;
        wins[i].bClose = false;
    }

    for(i = 0; i < len; i++){
        if(!initWindowAndRenderer(&(wins[i].win), &(wins[i].render))){
            printf("Create window[%d] and render failed!\n", i);
            return -1;
        }
        SDL_SetWindowResizable(wins[i].win, SDL_TRUE);
        wins[i].windowID = SDL_GetWindowID(wins[i].win);
        printf("Window wins[%d] init OK, windowID = %d.\n", i, wins[i].windowID);
    }

    return 1;
}

void winsEventsHandle(SdlWindow wins[], int len, SDL_Event *e){
    int i;
    for(i = 0; i < len; i++){
        if(e->type == SDL_WINDOWEVENT && e->window.windowID == wins[i].windowID){
            switch (e->window.event) {
            case SDL_WINDOWEVENT_SIZE_CHANGED:
                wins[i].width = e->window.data1;
                wins[i].height = e->window.data2;
                break;
            case SDL_WINDOWEVENT_EXPOSED:
                printf("wins[%d]: repaint on exposure.\n", i);
                break;
            case SDL_WINDOWEVENT_ENTER:
                printf("wins[%d]: mouse entered window.\n", i);
                break;
            case SDL_WINDOWEVENT_LEAVE:
                printf("wins[%d]: mouse leave window.\n", i);
                break;
            case SDL_WINDOWEVENT_FOCUS_GAINED:
                printf("wins[%d]: window has keyboard focus.\n", i);
                break;
            case SDL_WINDOWEVENT_FOCUS_LOST:
                printf("wins[%d]: window lost keyboard focus.\n", i);
                break;
            case SDL_WINDOWEVENT_MAXIMIZED:
                printf("wins[%d]: window minimized.\n", i);
                break;
            case SDL_WINDOWEVENT_MINIMIZED:
                printf("wins[%d]: window maximized.\n", i);
                break;
            case SDL_WINDOWEVENT_RESTORED:
                printf("wins[%d]: window restored.\n", i);
                break;
            case SDL_WINDOWEVENT_CLOSE:
                printf("wins[%d]: window closed.\n", i);
                wins[i].bClose = true;
                SDL_HideWindow(wins[i].win);
                break;
            }
        }
    }
}


